
import os
from FieldFormat import FieldFormat
from Config import EXCEL_DIR
from Config import TSCodeDir
from ScriptConfig import TS_CODE_CONFIG
from ScriptConfig import DICTIONARY_SCRIPT_CONFIG
from ScriptConfig import MANAGER_SCRIPT_CONFIG

class TSCodeGen:

    @staticmethod
    def Tab(count):
        return "    " * count

    # 代码生成函数
    @staticmethod
    def Process(filepath, fields, table):
        
        templateStr = TS_CODE_CONFIG
        filecontent = templateStr
       

        tablebasename = os.path.basename(filepath)
        tablebasename = tablebasename.split(".")[0]
        fieldListStr = ''
        filedInitStr = ''
        # 生成字段代码
        for index in fields:
            fielddesc = table.cell(2, index).value
            fieldtype = table.cell(1, index).value
            fieldname = table.cell(0, index).value
            fieldtype = fieldtype.lower()
            fieldvar = FieldFormat.Type2format[fieldtype][1]

            fieldListStr += TSCodeGen.Tab(1) + "/**  " +  fielddesc + " */\n"
            fieldListStr += TSCodeGen.Tab(1) + "public " + fieldname + ": "+ fieldvar + ";\n"

            filedInitStr += TSCodeGen.Tab(2) + "this." + fieldname + " = data[\"" + fieldname + "\"];\n"

        filecontent = filecontent.replace("[TS_FIELD_List]", fieldListStr)
        filecontent = filecontent.replace("[ExcelName]", tablebasename)
        filecontent = filecontent.replace("[TS_FIELD_INIT_CONFIG]",filedInitStr)
        

        MainFileName = ''
        for index in fields:
            fielddesc = table.cell(2, index).value
            fieldtype = table.cell(1, index).value
            fieldname = table.cell(0, index).value
            fieldtype = fieldtype.lower()
            if index == 0 :
                MainFileName = fieldname

        filecontent = filecontent.replace('[MainKeyFlag]','value.' + MainFileName)
        
        # 保存
        path = filepath.replace(EXCEL_DIR, "")
        path = TSCodeDir + "generate/" + path
        path = os.path.splitext(path)[0]
        path = path + ".ts"

        # 生成文件目录, 不重复创建目录
        filedir = os.path.dirname(path)
        if os.path.exists(filedir) == False:
            os.makedirs(filedir)

        print("生成脚本文件：",path)
        file = open(path, "wb")
        file.write(filecontent.encode())
        file.close()

    # 生成字典类脚本
    @staticmethod
    def GenDictionaryCode():
        path = TSCodeDir + "Dictionary.ts"
        filecontent = DICTIONARY_SCRIPT_CONFIG
        file = open(path, "wb")
        file.write(filecontent.encode())
        file.close()
    

    # 生成配置管理类
    @staticmethod
    def GenConfigMangerCode(files):
        path = TSCodeDir + "ConfigManager.ts"
        
        filecontent = MANAGER_SCRIPT_CONFIG

        tablebaseListStr = ''
        tablebaseImportListStr = ''
        index = 0
        for file in files:
            tablebasename = os.path.basename(file)
            tablebasename = tablebasename.split(".")[0]
            tablebaseListStr += TSCodeGen.Tab(2) + tablebasename + ".Deserialize(data[\""+ tablebasename + "\"]);\n"
            tablebaseImportListStr += "import " + tablebasename + " from \"./generate/" + tablebasename + "\";\n"
            index += 1

        filecontent = filecontent.replace("[Script_Import]",tablebaseImportListStr)
        filecontent = filecontent.replace("[Sheet_Deserialize]",tablebaseListStr)

        # 保存
        file = open(path, "wb")
        file.write(filecontent.encode())
        file.close()
